package com.jrx.anytxn.transaction.job.coupon.config;

import com.jrx.anytxn.common.constant.Constants;
import com.jrx.anytxn.customer.entity.CmCustomerBankcardInfo;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.config.CouponConfig;
import com.jrx.anytxn.transaction.job.coupon.dto.CustomerRightsDTO;
import com.jrx.anytxn.transaction.job.coupon.step.CompensationActivateProcessor;
import com.jrx.anytxn.transaction.job.coupon.step.CompensationActivateWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName CompensationActivateConfiguration
 *
 * @author lujianyou
 * @version 1.0
 * @description 补偿激活时未送120元优惠批处理
 * @date 2021/11/24 10:39
 **/
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class CompensationActivateConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private CompensationActivateProcessor compensationActivateProcessor;
    @Autowired
    private CompensationActivateWriter compensationActivateWriter;
    @Autowired
    private CouponConfig couponConfig;

    @Bean
    public Job compensationActivateJob(){
        return jobBuilderFactory.get("compensationActivateJob")
                .incrementer(new RunIdIncrementer())
                .start(compensationActivateStep())
                .build();
    }

    @Bean
    public Step compensationActivateStep() {
        return stepBuilderFactory.get("compensationActivateStep")
                .<CmCustomerBankcardInfo, CustomerRightsDTO>chunk(anytxnBatchProperties.getCompensationActivateConfig().getChunkLimit())
                //表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(compensationActivateReader())
                .processor(compensationActivateProcessor)
                .writer(compensationActivateWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                //多线程
                .throttleLimit(anytxnBatchProperties.getDefaultConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerBankcardInfo> compensationActivateReader() {
        MyBatisCursorItemReader<CmCustomerBankcardInfo> itemStreamReader = new MyBatisCursorItemReader<>();
        itemStreamReader.setSaveState(false);
        itemStreamReader.setSqlSessionFactory(sqlSessionFactory);
        //查询方法
        itemStreamReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerBankcardInfoMapper." +
                "listByCustomerIdNotInCouponDetail");
        Map<String,Object> map = new HashMap<>(8);

        //查询参数设置
        map.put("tenantId", Constants.DEFAULT_TENANT_ID);
        List<String> activityList = new ArrayList<>();
        activityList.add(couponConfig.getActivity().getChannelMissing());//通过默认渠道激活
        activityList.add(couponConfig.getActivity().getMGM());//通过邀新有礼激活
        Map<String, String> channels = couponConfig.getActivity().getChannels();//通过其他渠道激活
        for (Map.Entry<String,String> entry : channels.entrySet()){
            activityList.add(entry.getValue());
        }
        map.put("activityList", activityList);
        map.put("channel", Constants.DEFAULT_CHANNEL);
        map.put("cardType","0");
        itemStreamReader.setParameterValues(map);

        SynchronizedItemStreamReader<CmCustomerBankcardInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(itemStreamReader);
        return synchronizedItemStreamReader;
    }

}
